package com.educationx;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.quickaction.ActionItem;
import com.quickaction.QuickAction;
import com.util.PreferenceConnector;

public class CourseListOnItemClickListener implements OnItemClickListener {

	private Context context;
	private List<Course> items;
	private boolean enrolledcourses;

	public CourseListOnItemClickListener(Context context, List<Course> items, boolean enrolledcourses) {
		this.context = context;
		this.items = items;
		this.enrolledcourses = enrolledcourses;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		final String id = items.get(arg2).getId();	

		final QuickAction qa = new QuickAction(arg1);

		ActionItem actionEnroll = new ActionItem();
		if(enrolledcourses) {
			Intent i = new Intent(context, CourseActivity.class);
			i.putExtra("id", id);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
		} else {
			actionEnroll.setTitle("Subscribe");

			ActionItem actionDetails = new ActionItem();
			actionDetails.setTitle("Details");

			actionDetails.setOnClickListener(new OnClickListener() {	
				@Override
				public void onClick(View v) {
					Intent i = new Intent(context, CourseActivity.class);
					i.putExtra("id", id);
					i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(i);
					qa.dismiss();
				}
			});

			actionEnroll.setOnClickListener(new OnClickListener() {	
				@Override
				public void onClick(View v) {
					if(enrolledcourses) {
						Intent i = new Intent(context, CourseActivity.class);
						i.putExtra("id", id);
						i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(i);
					} else {
						Intent intent_comment_service = new Intent(context, EnrollService.class);
						Messenger messenger = new Messenger(handler);
						intent_comment_service.putExtra("messenger", messenger);
						intent_comment_service.putExtra("userID", PreferenceConnector.readString(context, "id", "0"));
						intent_comment_service.putExtra("courseID", id);
						context.startService(intent_comment_service);
					}
					qa.dismiss();
				}
			});

			qa.addActionItem(actionEnroll);
			qa.addActionItem(actionDetails);

			qa.setAnimStyle(QuickAction.ANIM_AUTO); 
			qa.show();
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message message) {
			if(message.obj == null) {
				Toast.makeText(context, "Failed to send data. Servers are temporarily " +
						" down.", Toast.LENGTH_LONG).show();
			}
			else if(message.arg1 == Services.ENROLL) {
				if(message.obj.toString().equals("1\n")) {
					Toast.makeText(context, "You have succesfully enrolled this course.", Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(context, "You have already enrolled this course.", Toast.LENGTH_LONG).show();
				}
			}
		}
	};
}
